// declare global {
	interface Math {
		/**
		 * 参数，最小值，最大值
		 * 参数介于最小值与最大值之间时取参数，参数小于最小值取最小值，反之取最大值
		 */
		limit: (v: number, min: number, max: number) => number;
		// return v1 <= num < v2;
		// _.random
		randomInt: (v1: number, v2: number) => number;
		randomGaussian: (v1: number, v2: number) => number;
		normal: (mu: number, sigma: number) => number;
		// random_zf(10) => v in [-10,10]
		randomZF: (v: number) => number;
		numberEqual(a: number, b: number): boolean;
	}
// }

function extendMath(): void {
	// console.log("extend math");
	Math.limit = (v: number, min: number, max: number): number => {
		if (v < min) return min;
		if (v > max) return max;
		return v;
	};
	Math.randomInt = (v1: number, v2: number): number => Math.floor(v1 + Math.random() * (v2 - v1));
	Math.randomZF = (v: number): number => Math.randomInt(-v, v + 1);
	Math.numberEqual = (a: number, b: number) => Math.abs(a - b) < Number.EPSILON;
	Math.normal = (mu: number, sigma: number): number => {
		let x = 0;
		let y = 0;
		let r = 0;
		do {
			x = Math.random() * 2 - 1;
			y = Math.random() * 2 - 1;
			r = x * x + y * y;
		} while (!r || r > 1);

		return mu + sigma * y * Math.sqrt((-2 * Math.log(r)) / r);
	};
	Math.randomGaussian = (v1: number, v2: number) => {
		const v = Math.normal((v1 + v2) / 2, (v2 - v1) / 6);
		return v < v1 || v > v2 ? Math.randomInt(v1, v2) : v;
	};
}
